package com.jsgt.greekfire.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.fhx.core.base.BaseViewModel
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.bean.PoolBean
import com.jsgt.greekfire.bean.PoolOrderBean
import com.jsgt.greekfire.bean.PrivilegeBean
import com.jsgt.greekfire.bean.PrivilegeWalletBean
import com.jsgt.greekfire.common.http.safeLaunch
import com.jsgt.greekfire.common.util.MD5Util
import com.jsgt.greekfire.repository.PrivilegeRepository

class PrivilegeViewModel : BaseViewModel() {
    private val cloudRepository = PrivilegeRepository.instance
    val errorLiveData by lazy { MutableLiveData<Any>() }
    val privilegeData by lazy { MutableLiveData<List<PrivilegeBean>>() }
    val poolListData by lazy { MutableLiveData<List<PoolBean>>() }
    val walletListData by lazy { MutableLiveData<List<PrivilegeWalletBean>>() }
    val transferListData by lazy { MutableLiveData<Any>() }
    val poolInData by lazy { MutableLiveData<Any>() }
    val poolOutData by lazy { MutableLiveData<Any>() }
    val poolOrderListData by lazy { MutableLiveData<List<PoolOrderBean>>() }

    /**
     * 云创智/云创联 -特权收益
     * type 0-云创智 1-云创联
     */
    fun getPrivilege(
        type: Int
    ) {
        viewModelScope.safeLaunch {
            block = {
                val data = cloudRepository.getPrivilege(type)
                if (data.code == 0) {
                    privilegeData.value = data.data!!
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }


    /**
     * 云矿池列表
     */
    fun getPoolList() {
        viewModelScope.safeLaunch {
            block = {
                val data = cloudRepository.poolList()
                if (data.code == 0) {
                    poolListData.value = data.data!!
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }


    /**
     * 转入云矿池/云矿场
     */
    fun poolIn(type: String, password: String, assetTypeId: Int, money: Double) {
        viewModelScope.safeLaunch {
            block = {
                val params = HashMap<String, Any>()
                params["type"] = type
                params["password"] = MD5Util.getMD5(password)
                params["assetTypeId"] = assetTypeId
                params["money"] = money
                val data = cloudRepository.poolIn(params)
                if (data.code == 0) {
                    poolInData.value = data.msg!!
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }



    /**
     * 转出云矿池/云矿场
     */
    fun poolOut(id: String, password: String) {
        viewModelScope.safeLaunch {
            block = {
                val params = HashMap<String, Any>()
                params["id"] = id
                params["password"] = MD5Util.getMD5(password)
                val data = cloudRepository.poolOut(params)
                if (data.code == 0) {
                    poolOutData.value = data.msg!!
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 获取池订单列表
     */
    fun poolOrderList(type: String) {
        viewModelScope.safeLaunch {
            block = {
                val data = cloudRepository.poolOrderList(type)
                if (data.code == 0) {
                    poolOrderListData.value = data.page!!.list!!
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 云创智/云创联 - 钱包
     * type 0-云创智 1-云创联
     */
    fun getPrivilegeWallet(
        type: Int
    ) {
        viewModelScope.safeLaunch {
            block = {
                val data = cloudRepository.getPrivilegeWallet(type)
                if (data.code == 0) {
                    walletListData.value = data.data!!
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }


    /**
     * 云创智/云创联 - 划转
     * type 0-云创智 1-云创联
     */
    fun privilegeWalletTransfer(
        type: Int,
        id: Int,
        amount: Double,
        payPassword: String
    ) {
        viewModelScope.safeLaunch {
            block = {
                val params = HashMap<String, Any>()
                params["id"] = id
                params["amount"] = amount
                params["tradingPassword"] = MD5Util.getMD5(payPassword)
                params["type"] = type
                val data = cloudRepository.privilegeWalletTransfer(params)
                if (data.code == 0) {
                    transferListData.value = data.msg!!
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }
}